package com.example.wanghao.wangyixinwen.fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.example.wanghao.wangyixinwen.act.MainActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wanghao on 2016/5/17.
 */
public class MenuFragment extends Fragment implements AdapterView.OnItemClickListener {

    private Context mContext;//上下文

    //初始化方法
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ListView lv = new ListView(mContext);
        ArrayAdapter adapter = new ArrayAdapter(mContext, android.R.layout.test_list_item,
                android.R.id.text1,initDatas());
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(this);

        return lv;
    }

    private List initDatas() {
        List<String> datas = new ArrayList<>();
        datas.add("页面1");
        datas.add("页面2");
        datas.add("页面3");
        return datas;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //点击对应位置的条目,让主界面切换到不同界面
        Fragment fragment = null;
        switch (position){
            case 0:
                fragment = new Fragment1();
                break;
            case 1:
                fragment = new Fragment2();
                break;
            case 2:
                fragment = new Fragment3();
                break;
        }
        //让主界面切换到对应的fragment界面

       /*MainActivity main = new MainActivity();
       main.swithFragment(fragment);*/
        if (mContext instanceof MainActivity){
            ((MainActivity)mContext).swithFragment(fragment);
        }

    }
}
